<?php

/**
 *    Desc
 *
 *    @author    Garbin
 *    @usage    none
 */
class MemberApp extends MemberbaseApp
{
	
    function __construct()
    {
        $this->MemberApp();
    }
    function MemberApp()
    {
        parent::__construct();
		$user = $this->visitor->get();
		$user_mod =& m('member');
		$this->check_powers(USER_ADMIN);
    }
    function index()
    {
        $user_mod =& m('member');
		$page = $this->_get_page();
		$users = $user_mod->find(array(
			'conditions' => "role not in (".USER_CLIENT.','.USER_FACTORY.','.USER_MERCHANT.")",
			'count' => true,
			'limit' => $page['limit'],
			'order'=>'role,is_factory'
		));
		$page['item_count'] = $user_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
        $this->assign('users', $users);
        $this->display('member.index.html');
        /* 当前位置 */
        $this->_curlocal(LANG::get('member_manage'));
    }
    
    /**
     * 用户管理
     */
    function user_list(){
    	$user_mod =& m('member');
    	$type=empty($_GET['utype'])?'':trim($_GET['utype']);
    	$conditions=$this->_get_query_conditions(array(
    		array(
    			'field' => 'user_name',
                'equal' => 'like',		
    			'name'	=> 'user_name'
    		),
    		array(
    			'field' => 'email',
    			'equal' => 'like',
    			'name'	=> 'email'
    		),
    		array(
    			'field' => 'phone_mob',
    			'equal' => 'like',
    			'name'	=> 'phone_mob'
    		),
    		array(
    			'field' => 'is_qq',
    			'equal' => 'like',
    			'name'	=> 'im_qq'
    		)
    	));
    	$page = $this->_get_page();
    	$role=USER_CLIENT;
    	if ($type=='factory'){
    		$role=USER_FACTORY;
    		$this->assign('utype',$type);
    	}elseif ($type=='merchant'){
    		$role=USER_MERCHANT;
    		$this->assign('utype',$type);
    	}
    	$conditions .=" and role=".$role;
    	$order = empty($_GET['order']) ? 'reg_time DESC' : str_replace('-', ' ', trim($_GET['order']));
    	if ($order != 'reg_time DESC' || $order != 'reg_time ASC') {
    		$order .= ', reg_time DESC';
    	}
    	$users = $user_mod->find(array(
    			'conditions'=>'1=1'.$conditions,
    			'fields'=>'*,freight_id as merch_name',
    			'order' => $order,
    			'count' => true,
    			'limit' => $page['limit'],
    	));
    	if (!empty($users)){
    		foreach ($users as $k=>$v){
    			$merch_id=$v['freight_id'];
    			$merch_info=$user_mod->get($merch_id);
    			$users[$k]['merch_name']=$merch_info['user_name'];
    		}
    	}
    	$page['item_count'] = $user_mod->getCount();
    	$this->_format_page($page);
    	$this->assign('page_info', $page);
    	$this->assign('users', $users);
    	/* 当前位置 */
    	$this->_curlocal(LANG::get('user_manage'));
    	$this->display('member.user.html');
    }
	
    /* 禁止使用用户 */
    function forbid_user(){
    	if ($this->visitor->get('role')!=USER_ADMIN){
    		$this->show_warning('user_no_acl');
    		return;
    	}
    	$user_mod=&m('member');
    	$user_id=empty($_GET['uid'])?0:intval($_GET['uid']);
	    if (!$user_id) {
			$this->show_warning('user_not_exist');
			return;
		}
		$user = $user_mod->get_info($user_id);
		if (!$user) {
			$this->show_warning('user_not_exist');
			return;
		}
		if (!$user_mod->edit($user_id,array('activation'=>0))){
			$this->show_warning('forbid_user_error');
			return;
		}
		$this->show_message('forbid_user_success');
    }
    
    
    /* 激活使用用户 */
    function activation_user(){
    	if (!in_array($this->visitor->get('role'), array(USER_ADMIN,USER_MERCHANDISER,USER_CIVILIAN))){
    		$this->show_warning('user_no_acl');
    		return;
    	}
    	$user_mod=&m('member');
    	$user_id=empty($_GET['uid'])?0:intval($_GET['uid']);
    	if (!$user_id) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	$user = $user_mod->get_info($user_id);
    	if (!$user) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	if (!$user_mod->edit($user_id,array('activation'=>1))){
    		$this->show_warning('activation_user_error');
    		return;
    	}
    	$mail = get_mail('tobuyer_send_activation', array('user' =>$user));
    	$this->_mailto($user['email'], addslashes($mail['subject']), addslashes($mail['message']));
    	$this->show_message('activation_user_success');
    }
    
	/* 添加新用户 */
	function add()
	{
		$addtype = trim($_GET['addtype']);
		$user_mod =& m('member');
		$factory_mod=&m("factory");
		if($addtype=="factory")
		{
			$factory_list=$factory_mod->find(array(
			"conditions"=>"factory.factory_id not in(select factory_id from $user_mod->table)",
			"fields"=>"factory.factory_id,factory_name"));
		$this->assign("factory_list",$factory_list);

		}
		if (!IS_POST) {
			$this->_assign_form();
			$this->_assign_merchandiser();
			$this->assign('belongs_member_img',BELONGS_MEMBER_IMG);
			$this->assign('default_cust_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->display('member.form.html');
			$this->_curlocal(LANG::get('member_manage'), 'index.php?app=member', Lang::get('add_new_user'));
		} else {
			$user_name = trim($_POST['user_name']);
			
			$email = trim($_POST['email']);
			$password = trim($_POST['password']);
			$password2 = trim($_POST['password2']);
			$role = intval($_POST['role']);
			$activation = intval($_POST['activation']);
			$is_factory = $role==USER_FACTORY?1:0;	//当员工角色为工厂时是工厂类型
			$factory_id = isset($_POST['factory_id'])?intval($_POST['factory_id']):0;
			
			if($addtype=="factory")
			{	
				$factory_info=$factory_mod->get("factory_id={$factory_id}");
				if (empty($factory_info)) {
				  $this->show_warning('请选择对应的工厂');
				  return;
				 }
				 $user_name=$factory_info['factory_name'];
				 $role=USER_FACTORY;
			}

			if (empty($user_name)) {
				  $this->show_warning('user_name_not_empty');
				  return;
			 }
			  
			if (!$user_mod->unique($user_name)) {
				$this->show_warning('user_name_is_exist');
				return;
			}

			if (empty($password)) {
				$this->show_warning('password_not_empty');
				return;
			}
			if (empty($password2)) {
				$this->show_warning('confirm_password_not_empty');
				return;
			}
			if (strlen($password) < 6 || strlen($password) > 12) {
				$this->show_warning('password_length_error');
				return;
			}
			if ($password != $password2) {
				$this->show_warning('password_not_eq_confirm');
				return;
			}
			if (in_array($role, array(USER_CLIENT,USER_FACTORY))) {
				$this->show_warning('role_select_pls');
				return;
			}
			
			$data = array(
				'user_name' => $user_name,
				'ch_name'=>trim($_POST['ch_name']),
				'password' => md5($password),
				'role' => $role,
				'activation' => $activation,
				'is_factory' => $is_factory,
				'email' => $email,
				'address'=>trim($_POST['address']),
				'phone_mob'=>trim($_POST['phone_mob']),
				'phone_tel'=>trim($_POST['phone_tel']),
				'is_qq'	=>trim($_POST['im_qq']),
				'factory_id'=>$factory_id
			);
			
			if ($role == USER_ASSISTANT) {
				$temp = trim($_POST['merchandiser']);
				$temp = explode(',', $temp);
				foreach ($temp as $key => $val) {
					$val = intval($val);
					if (!$val) {
						unset($temp[$key]);
					} else {
						if (in_array($val, $merchandiser)) {
							unset($merchandiser[$key]);
						} else {
							$merchandiser[$key] = $val;
						}
					}
				}
				$data['merchandiser'] = implode(',', $merchandiser);
			}
			
			$user_mod =& m('member');
			$user_id = $user_mod->add($data);
			if (!$user_id) {
				$this->_error($user_mod->get_error());
				return false;
			}
			
			$member_img_id=isset($_POST['member_img_id'])?$_POST['member_img_id']:'';
			$file_mod =& m('upload');
			if(!empty($member_img_id))
			{
				$file_ids=implode(",",$member_img_id);
				$cust_files_info=$file_mod->find(array(
						"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_MEMBER_IMG." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
								));
					
				foreach($cust_files_info as $item)
				{
					$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $user_id, $item['file_path']);
					if(!file_exists(dirname($file_path))) {
						mkdir(dirname($file_path), 0777);
					}
					rename($item['file_path'], $file_path);
					$file_mod->edit("file_id={$item['file_id']}",array(
							"file_path"=>$file_path,
							"key_id"=>$user_id,
							"session_id"=>session_id()
					));
				}
			}
			$this->show_message('add_user_success', Lang::get('member_center'), 'index.php?app=member');
		}
	}
	
	function _assign_form()
	{
		$user = array(
			'role' => 2,
			'if_show' => 1
		);
		$this->assign('user', $user);
	}
	
	function _assign_merchandiser($ids = '')
	{
		$user_mod =& m('member');
		$list = $user_mod->find(array(
			'conditions' => 'if_show = 1 and activation=1 AND role = ' . USER_MERCHANDISER,// . ($ids ? ' AND user_id not in(' . $ids . ')' : ''),
			'order' => 'user_name',
		));
		$this->assign('merchandiser_list', $list);
		if ($ids) {
			$list = $user_mod->find(array(
				'conditions' => 'if_show = 1 and activation=1 AND role = ' . USER_MERCHANDISER . ($ids ? ' AND user_id in(' . $ids . ')' : ''),
				'order' => 'user_name',
			));
			$this->assign('merchandiser_list1', $list);
		}
	}
	
	function edit()
	{
		$user_mod =& m('member');
		$user_id = intval($_GET['uid']);
		if (!$user_id) {
			$this->show_warning('user_not_exist');
			return;
		}
		$user = $user_mod->get_info($user_id);
		if (!$user) {
			$this->show_warning('user_not_exist');
			return;
		}
		if(!IS_POST) {			
			$file_mod=&m('upload');
			$user['member_img']=$file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_MEMBER_IMG . ' AND key_id = ' . $user_id,
					'order' => 'file_id ASC',
			));
			$this->assign('user', $user);
			$this->assign('role_merch',USER_MERCHANDISER);
			$this->assign('belongs_member_img',BELONGS_MEMBER_IMG);
			$this->_assign_merchandiser($user['merchandiser']);
			$this->display('member.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('member_manage'), 'index.php?app=member', Lang::get('edit_user'));
		} else {
			$user_name = trim($_POST['user_name']);
			$old_pwd = $_POST['old_pwd'];
			$password = $_POST['password'];
			$password2 = $_POST['password2'];
			$role = intval($_POST['role']);
			$activation = intval($_POST['activation']);
			$email = trim($_POST['email']);
			$is_factory = $role==USER_FACTORY?1:0;	//当员工角色为工厂时是工厂类型
			if(!$user['is_factory'])
			{	
			  if (empty($user_name)) {
				  $this->show_warning('user_name_not_empty');
				  return;
			  }
			  if (!$user_mod->unique($user_name, $user_id)) {
				  $this->show_warning('user_name_is_exist');
				  return;
			  }
			}
			if (!empty($password) && !empty($password2) && md5($password) != $old_pwd) {
				if (empty($password)) {
					$this->show_warning('password_not_empty');
					return;
				}
				if (empty($password2)) {
					$this->show_warning('confirm_password_not_empty');
					return;
				}
				if (strlen($password) < 6 || strlen($password) > 12) {
					$this->show_warning('password_length_error');
					return;
				}
				if ($password != $password2) {
					$this->show_warning('password_not_eq_confirm');
					return;
				}
				$new_pwd = md5($password);
			}
			
			if(!$user['is_factory'])
			{	
			  if ($role < USER_ADMIN || $role > 100) {
				  $this->show_warning('role_select_pls');
				  return;
			  }
			}
			$data = array(
				'user_name' => $user_name,
				'ch_name'=>trim($_POST['ch_name']),
				'password' => empty($new_pwd) ? $old_pwd : $new_pwd,
				'role' => $role,
				'email' => $email,
				'address'=>trim($_POST['address']),
				'phone_mob'=>trim($_POST['phone_mob']),
				'phone_tel'=>trim($_POST['phone_tel']),
				'is_qq'	=>trim($_POST['im_qq']),
				'is_factory' => $is_factory,
				'activation' => $activation
			);
			
			if($user['is_factory'])
			{	
				//unset($data['user_name']);
				//unset($data['role']);
				unset($data['is_factory']);
			}
			
			if ($role == USER_MERCHANDISER){
				$data['role_cate']=trim($_POST['role_cate']);
				$data['default_merch']=trim($_POST['default_merch']);
				if ($data['default_merch']){
					$user_mod->edit('default_merch=1',array('default_merch'=>0));
				}
			}else{
				$data['role_cate']=0;
				$data['default_merch']=0;
			}
			
			if ($role == USER_ASSISTANT) {
				$temp = trim($_POST['merchandiser']);
				$temp = explode(',', $temp);
				foreach ($temp as $key => $val) {
					$val = intval($val);
					if (!$val) {
						unset($temp[$key]);
					} else {
						if (in_array($val, $merchandiser)) {
							unset($merchandiser[$key]);
						} else {
							$merchandiser[$key] = $val;
						}
					}
				}
				$data['merchandiser'] = implode(',', $merchandiser);
			}
			$user_mod->edit($user_id, $data);
			if($user_mod->get_error()) {
				$this->_error($user_mod->get_error());
                return;
			}
			$this->show_message('edit_user_success', Lang::get('member_center'), 'index.php?app=member');
		}
	}
	
	//删除用户
	function del()
	{
		$user_mod =& m('member');
		$user_id = intval($_GET['uid']);
		if (!$user_id) {
			$this->show_warning('user_not_exist');
			return;
		}
		$user_info=$user_mod->get_info($user_id);
		if (empty($user_info)) {
			$this->show_warning('user_not_exist');
			return;
		}
		if ($user_info['role']==USER_FACTORY){
			$consumer_mod=&m('factory_info');
			$file_mod =& m('upload');
			$factory_files = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_FACTORY_IMAGE . ' AND key_id = ' . $user_id,
					'order' => 'file_id ASC',
			));
			if (!empty($factory_files)){
				foreach($factory_files as $item)
				{
					if (!$file_mod->drop($item['file_id'])) {
						$this->show_warning('删除文件失败');
						return;
					} else {
						if (file_exists($item['file_path'])) {
							unlink($item['file_path']);
						}
					}
				}
			}
		}elseif ($user_info['role']==USER_MERCHANT){
			$consumer_mod=&m('fabric_shop');
		}else{
			$consumer_mod=&m('consumer_info');
		}
		$user_mod->drop("user_id={$user_id}");
		if($user_mod->get_error()) {
			$this->_error($user_mod->get_error());
			return;
		}
		$consumer_mod->drop("user_id={$user_id}");
		if($consumer_mod->has_error()) {
			$this->show_warning($consumer_mod->get_error());
			return;
		}
		$this->show_message('删除用户成功');
		
	}
	
    /**
     *    检查用户是否存在
     *
     *    @author    Garbin
     *    @return    void
     */
    function check_user()
    {
		$user_name = empty($_GET['user_name']) ? null : trim($_GET['user_name']);
		$id = empty($_GET['uid']) ? 0 : intval($_GET['uid']);
		if (!$user_name) {
            echo ecm_json_encode(false);
            return;
		}
		$user_mod =& m('member');
		echo ecm_json_encode($user_mod->unique($user_name, $id));
        /*$user_name = empty($_GET['user_name']) ? null : trim($_GET['user_name']);
		$id = empty($_GET['uid']) ? 0 : intval($_GET['uid']);
        if (!$user_name)
        {
            echo ecm_json_encode(false);

            return;
        }
        $ms =& ms();

        echo ecm_json_encode($ms->user->check_username($user_name, $id));*/
    }
     /**
     *    三级菜单
     *
     *    @author    Hyber
     *    @return    void
     */
    /*function _get_member_submenu()
    {
        $submenus =  array(
            array(
                'name'  => 'edit_password',
                'url'   => 'index.php?app=member&amp;act=password',
            ),
        );

        return $submenus;
    }*/

    /**
     * 上传头像
     *
     * @param int $user_id
     * @return mix false表示上传失败,空串表示没有上传,string表示上传文件地址
     */
    function _upload_portrait($user_id)
    {
        $file = $_FILES['portrait'];
        if ($file['error'] != UPLOAD_ERR_OK)
        {
            return '';
        }
        import('uploader.lib');
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE);
        $uploader->addFile($file);
        if ($uploader->file_info() === false)
        {
            $this->show_warning($uploader->get_error(), 'go_back', 'index.php?app=member&amp;act=profile');
            return false;
        }
        $uploader->root_dir(ROOT_PATH);
        return $uploader->save('data/files/mall/portrait/' . ceil($user_id / 500), $user_id);
    }
	
	//角色管理
	function rolesman()
	{
		$mod_roles = &af("roles");
        $roles = $mod_roles->getAll(); //载入系统设置数据
		$this->assign("role_list",$roles);
		$this->display('roles.index.html');
	}
	
	//角色编辑
	function editrole()
	{
	  $role_name=isset($_GET['role_name'])?trim($_GET['role_name']):"";
	  
	  if(empty($role_name))
	  {
		  $this->show_warning("role_not_exist",'go_back', 'javascript:history.go(-1);',LANG::GET("retrun_role_list"),"index.php?app=member&act=rolesman");
		  return;
	  }
	  if($role_name=="系统管理员")
	  {
		  $this->show_warning("不能编辑系统管理员的权限！",'go_back', 'javascript:history.go(-1);',LANG::GET("retrun_role_list"),"index.php?app=member&act=rolesman");
		  return;
	  }

	  $mod_roles = &af("roles");
	  $role_info=array("role_name"=>$role_name,"acl"=>$mod_roles->getone($role_name));
	  if(empty($role_info))
	  {
		  $this->show_warning("role_not_exist",'go_back', 'javascript:history.go(-1);',LANG::GET("retrun_role_list"),"index.php?app=member&act=rolesman");
		  return;
	  }

	  if(!IS_POST)
	  {
		$menus=$this->_get_acl_menus();
		$role_info['acl']=explode(",",$role_info['acl']);
		$this->assign("role_info",$role_info);
		$this->assign("menus",$menus);
		$this->display('roles.edit.html');
	  }
	  else
	  {

		  $acl=isset($_POST['acl'])&&!empty($_POST['acl'])?implode(",",$_POST['acl']):"";
		  $mod_roles->setOne($role_name,$acl);
		  if($mod_roles->has_error())
		  {
			  $this->show_warning($mod_roles->get_error(),'go_back', 'javascript:history.go(-1);',LANG::GET("retrun_role_list"),"index.php?app=member&act=rolesman");
			  return;
		  }
		  
		  $this->show_message("edit_role_success",LANG::GET("retrun_role_list"),"index.php?app=member&act=rolesman");
	  }
	}
	
	//获取权限列表视图
	function _get_acl_menus()
	{
		$acl_config=include(ROOT_PATH . '/includes/access/acl.config.php');
		return $acl_config;
	}
	
	//提示用户无权限
	function user_noacl()
	{
		 $this->show_warning("user_no_acl",LANG::GET("back_index"),"index.php");
		
	}
	
	//财务密码设置
	function financepwdset()
	{
		$model_setting = &af('settings');
        $setting = $model_setting->getAll(); //载入系统设置数据
        if (!IS_POST)
        {
            $this->assign('setting', $setting);
            $this->assign('mail_type', array(
                MAIL_PROTOCOL_SMTP  => Lang::get('smtp'),
                MAIL_PROTOCOL_LOCAL => Lang::get('email'),
            ));
            $this->display('financepwdset.html');
        }
        else
        {
			if(isset($_POST['financepwd'])&&!empty($_POST['financepwd']))
			{
            $financepwd    =trim($_POST['financepwd']);
			$financepwd=md5($financepwd);
            $model_setting->setOne('financepwd',$financepwd);
			}
            $this->show_message('设置财务密码成功');
        }
			
	}
	
	//检查财务密码
	function checkfinpwd()
	{
		$model_setting = &af('settings');
        $setting = $model_setting->getAll(); //载入系统设置数据
        if (!IS_POST)
        {
            $this->assign('setting', $setting);
            $this->assign('mail_type', array(
                MAIL_PROTOCOL_SMTP  => Lang::get('smtp'),
                MAIL_PROTOCOL_LOCAL => Lang::get('email'),
            ));
            $this->display('financepwdset.html');
        }
        else
        {
			if(isset($_POST['financepwd'])&&!empty($_POST['financepwd']))
			{
            $financepwd    =trim($_POST['financepwd']);
			$financepwd=md5($financepwd);
            $model_setting->setOne('financepwd',$financepwd);
			}
            $this->show_message('设置财务密码成功');
        }
			
	}
	
	 /**
     *    修改密码
     *
     *    @author    Hyber
     *    @usage    none
     */
    function password(){
        $user_id = $this->visitor->get('user_id');
        if (!IS_POST)
        {
            /* 当前位置 */
            $this->_curlocal(LANG::get('member_center'),  'index.php?app=member',
                             LANG::get('edit_password'));

            /* 当前用户中心菜单 */
            $this->_curitem('my_profile');

            /* 当前所处子菜单 */
            $this->_curmenu('edit_password');
            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js',
            ));
            $this->_config_seo('title', Lang::get('user_center') . ' - ' . Lang::get('edit_password'));
            $this->display('member.password.html');
        }
        else
        {
            /* 两次密码输入必须相同 */
            $orig_password      = $_POST['orig_password'];
            $new_password       = $_POST['new_password'];
            $confirm_password   = $_POST['confirm_password'];
            if ($new_password != $confirm_password)
            {
                $this->show_warning('twice_pass_not_match');

                return;
            }
            if (!$new_password)
            {
                $this->show_warning('no_new_pass');

                return;
            }
            $passlen = strlen($new_password);
            if ($passlen < 6 || $passlen > 20)
            {
                $this->show_warning('password_length_error');

                return;
            }

            /* 修改密码 */
            $ms =& ms();    //连接用户系统
            $result = $ms->user->edit($this->visitor->get('user_id'), $orig_password, array(
                'password'  => $new_password
            ));
            if (!$result)
            {
                /* 修改不成功，显示原因 */
                $this->show_warning($ms->user->get_error());

                return;
            }

            $this->show_message('edit_password_successed');
        }
    }
    
    /* 检查邮箱是否已存在 */
    function check_email(){
    	$email=empty($_GET['email'])?'':trim($_GET['email']);
    	$user_id=empty($_GET['id'])?0:trim($_GET['id']);
    	if (!$email){
    		echo ecm_json_encode(false);
            return;
    	}
    	$user_mod =& m('member');
    	$email_info=$user_mod->get(array(
    		'conditions'=>"email='{$email}' and user_id <>".$user_id		
    	));
    	if (!empty($email_info)){
    		echo ecm_json_encode(false);
    		return;
    	}
    	echo ecm_json_encode(true);
    }
    
    
    /* 编辑客户信息 */
    function edit_user(){
    	if (!in_array($this->visitor->get('role'), array(USER_ADMIN,USER_MERCHANDISER))){
    		$this->show_warning('user_no_acl');
    		return;
    	}
    	$user_mod =& m('member');
    	$user_id = intval($_GET['uid']);
    	if (!$user_id) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	$user = $user_mod->get(array(
    		'conditions'=>'member.user_id='.$user_id,
    		'join'=>'has_consumer'		
    	));
    	if (!$user) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	if(!IS_POST) {
    		$region_mod=& m('region');
    		//查询面料类型
    		$freight_list=$user_mod->find(array(
    			'conditions'=>'activation=1 and role='.USER_MERCHANDISER,
    			'order'		=>'reg_time'	
    		));
    		$this->assign('freights',$freight_list);
    		//所属区域下拉数据
    		$this->assign('regions',$region_mod->get_options(0));
    		//查询公司及该公司所属的全部品牌
    		if ($user['company_id']){
    			$company_mod=& m('company');
    			$brand_arr=array();
    			$company_info=$company_mod->get(array(
    				'conditions'=>'company_id='.$user['company_id']	
    			));
    			$this->assign('company',$company_info);
    			$brand_mod=& m('brand');
    			$brand_list=$brand_mod->find(array(
    				'conditions'=>'company_id='.$user['company_id'],
    				'order'=>'brand_id asc',
    				'limit'=>'10'	
    			));
    			if ($user['brand_arr_id']){
    				$brand_arr=explode(',', $user['brand_arr_id']);
    			}
    			$this->assign('brand_arr',$brand_arr);
    			$this->assign('brand_list',$brand_list);
    		}
    		$this->assign('user', $user);
    		$this->assign('role_info',$this->visitor->get());
    		$this->display('consumer.form.html');
    		/* 当前位置 */
    		if ($this->visitor->get('role')==USER_ADMIN){
    			$this->_curlocal(LANG::get('user_manage'), 'index.php?app=member&act=user_list', Lang::get('edit_user'));
    		}else{
    			$this->_curlocal(LANG::get('user_manage'), 'index.php?app=schedule&act=consumer_list', Lang::get('edit_user'));
    		}
    	}else{
    		$email     = trim($_POST['email']);
    		$phone_mob = trim($_POST['phone_mob']);
    		$address   = trim($_POST['address']);
    		$local_data=array();
    		$register_data=array();
    		$local_data['phone_mob']=$phone_mob;
    		$local_data['address']=$address;
    		$local_data['is_factory']=0;
    		$local_data['role']=USER_CLIENT;
    		$local_data['email']=$email;
    		$register_data['consumer_name']=trim($_POST['consumer_name']);
    		$local_data['phone_tel']=trim($_POST['phone_tel']);
    		$local_data['is_qq']=trim($_POST['is_qq']);
    		$register_data['country']=trim($_POST['country']);
    		if ($_POST['region_id']){
    			$local_data['region_id']=trim($_POST['region_id']);
    			$local_data['region_name']=trim($_POST['region_name']);
    		}
    		$register_data['user_type']=trim($_POST['user_type']);
    		$register_data['is_vip']=trim($_POST['is_vip']);
    		//$register_data['cooperate']=trim($_POST['cooperate']);
    		$local_data['freight_id']=trim($_POST['freight_id']);
    		if (in_array($this->visitor->get('role'), array(USER_ADMIN,USER_MERCHANDISER))){
    			$local_data['user_name']=trim($_POST['user_name']);
    			if (!$user_mod->unique($user_name, $user_id)){
    				$this->show_warning('该用户名已存在');
    				return;
    			}
    		}
    		if (!$address || strlen($address)>100){
    			$this->show_warning('address_error');
    			return;
    		}
    		
    		if (!empty($_POST['password']))
    		{
    			$password = trim($_POST['password']);
    			if (strlen($password) < 6 || strlen($password) > 20)
    			{
    				$this->show_warning('password_length_error');
    				return;
    			}
    		}
    		if (!is_email(trim($_POST['email'])))
    		{
    			$this->show_warning('email_error');
    			return;
    		}
    		!empty($_POST['password']) && $local_data['password'] = md5(trim($_POST['password']));
    		if ($user['freight_id']!=trim($_POST['freight_id'])){
    			$freight_id=trim($_POST['freight_id']);
    			$freight_old_id=$user['freight_id'];
    			$merch_info=$user_mod->get(array('conditions'=>'user_id='.$freight_id));
    		}
    		$user_mod->edit($user_id,$local_data);
    		if ($user_mod->has_error()){
    			$this->show_warning('edit_user_error');
    			return;
    		}
    		$consumer_mod=& m('consumer_info');
    		$consumer_info=$consumer_mod->get(array(
    			'conditions'=>'user_id='.$user_id	
    		));
    		if (empty($consumer_info)){
    			$register_data['user_id']=$user_id;
    			if(!$consumer_mod->add($register_data)){
    				$this->show_warning($consumer_mod->get_error());
    				return;
    			}
    		}else{
    			$consumer_mod->edit('user_id='.$user_id,$register_data);
    			if ($consumer_mod->has_error()){
    				$this->show_warning('edit_consumer_error');
    				return;
    			}
    		}
    		
    		if ($freight_id){
    			$order_mod=&m('order');
    			$sample_mod=&m('sample');
    			$order_mod->edit('merchandiser_id='.$freight_old_id.' and status not '.db_create_in(array(ORDER_STATUS_CONFIRM,ORDER_STATUS_NO_CANCEL,ORDER_STATUS_SHIPPED,ORDER_STATUS_PAY_MONEY)).' and buyer_id='.$user_id,array('merchandiser_id'=>$freight_id,'merchandiser'=>$merch_info['user_name']));
    			if ($order_mod->has_error()){
    				$this->show_warning($order_mod->get_error());
    				return;
    			}
    			$sample_mod->edit('merchandiser_id='.$freight_old_id.' and status not'.db_create_in(array(SAMPLE_STATUS_DELETE,SAMPLE_STATUS_SEND_SAMPLE,SAMPLE_STATUS_STOCK)).' and buyer_id='.$user_id,array('merchandiser_id'=>$freight_id));
    			if ($sample_mod->has_error()){
    				$this->show_warning($sample_mod->get_error());
    				return;
    			}
    		}
    		
    		if ($this->visitor->get('role')==USER_ADMIN){
    			$this->show_message('edit_consumer_success', Lang::get('user_manage'), 'index.php?app=member&act=user_list');
    		}else{
    			$this->show_message('edit_consumer_success', Lang::get('user_manage'), 'index.php?app=schedule&act=consumer_list');
    		}
    	}
    }
    
    
    /* 编辑工厂信息 */
    function edit_factory(){
    	if (!in_array($this->visitor->get('role'), array(USER_ADMIN,USER_CIVILIAN))){
    		$this->show_warning('user_no_acl');
    		return;
    	}
    	$upload_mod =& m('upload');
    	$user_mod =& m('member');
    	$user_id = intval($_GET['uid']);
    	if (!$user_id) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	$user = $user_mod->get(array(
    			'conditions'=>'member.user_id='.$user_id,
    			'join'=>'has_factory'
    	));
    	if (!$user) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	if(!IS_POST) {
    		$region_mod=& m('region');
    		$this->assign('regions',$region_mod->get_options(0));
    		$this->assign('user', $user);
    		//赋值session_id
    		$this->assign('phpsision', session_id());
    		//查询当前session_id下的上传图片
    		$img_file=$upload_mod->find(array(
    				'conditions'=>"file_type=".BELONGS_FACTORY_IMAGE." and key_id=".$user['factory_id']
    		));
    		if ($img_file){
    			$this->assign('files',$img_file);
    		}
    		$this->display('factory_consumer.form.html');
    		/* 当前位置 */
    		if ($this->visitor->get('role')==USER_ADMIN){
    			$this->_curlocal(LANG::get('user_manage'), 'index.php?app=member&act=user_list', Lang::get('edit_user'));
    		}else{
    			$this->_curlocal(LANG::get('user_manage'), 'index.php?app=factory_list&act=activation', Lang::get('edit_user'));
    		}
    	}else{
    		$email     = trim($_POST['email']);
    		$phone_mob = trim($_POST['phone_mob']);
    		$address   = trim($_POST['address']);
    		$local_data=array();
    		$local_data['phone_mob']=$phone_mob;
    		$local_data['phone_tel']=trim($_POST['phone_tel']);
    		$local_data['address']=$address;
    		$local_data['email']=$email;
    		$local_data['region_id']=trim($_POST['region_id']);
    		$local_data['region_name']=trim($_POST['region_name']);
    		$local_data['is_qq']=trim($_POST['is_qq']);
    		$register_data=array();
    		$register_data['factory_name']=trim($_POST['factory_name']);
    		$register_data['factory_charge']=trim($_POST['factory_charge']);
    		//$register_data['cooperate']=trim($_POST['cooperate']);
    		$register_data['factory_goods_num']=trim($_POST['factory_goods_num']);
    		$register_data['bank_account']=trim($_POST['bank_account']);
    		$register_data['bank_user']=trim($_POST['bank_user']);
    		$register_data['bank_name']=trim($_POST['bank_name']);
    		if (in_array($this->visitor->get('role'), array(USER_ADMIN,USER_CIVILIAN))){
    			$local_data['user_name']=trim($_POST['user_name']);
    			if (!$user_mod->unique($user_name, $user_id)){
    				$this->show_warning('该用户名已存在');
    				return;
    			}
    		}
    		
    		if (empty($_POST['region_name'])){
    			$this->show_warning('region_required');
    			
    			return;
    		}
    		
    		if (!$address || strlen($address)>100){
    			$this->show_warning('address_error');
    			return;
    		}
    
    		if (!empty($_POST['password']))
    		{
    			$password = trim($_POST['password']);
    			if (strlen($password) < 6 || strlen($password) > 20)
    			{
    				$this->show_warning('password_length_error');
    				return;
    			}
    		}
    		if (!is_email(trim($_POST['email'])))
    		{
    			$this->show_warning('email_error');
    			return;
    		}
    		!empty($_POST['password']) && $local_data['password'] = md5(trim($_POST['password']));
    		$user_mod->edit($user_id,$local_data);
    		if ($user_mod->has_error()){
    			$this->show_warning('edit_user_error');
    			return;
    		}
    		$factory_info_mod=& m('factory_info');
    		$factory_info=$factory_info_mod->get(array(
    				'conditions'=>'user_id='.$user_id
    		));
    		if (empty($factory_info)){
    			$register_data['user_id']=$user_id;
    			$factory_id=$factory_info_mod->add($register_data);
    			if(!$factory_id){
    				$this->show_warning($factory_info_mod->get_error());
    				return;
    			}
    		}else{
    			$factory_id=$factory_info['factory_id'];
    			$factory_info_mod->edit('user_id='.$user_id,$register_data);
    			if ($factory_info_mod->has_error()){
    				$this->show_warning('edit_factory_info_error');
    				return;
    			}
    		}
    		$upfiles=$_POST['upfiles'];
    		
    		if(!empty($upfiles))
    		{
    			$upload_mod->edit("file_id ".db_create_in($upfiles),array('key_id'=>$factory_id,'session_id'=>session_id(),'file_type'=>BELONGS_FACTORY_IMAGE));
    		}
    		if ($this->visitor->get('role')==USER_ADMIN){
    			$this->show_message('edit_factory_info_success', Lang::get('user_manage'), 'index.php?app=member&act=user_list&utype=factory');
    		}else{
    			$this->show_message('edit_factory_info_success', Lang::get('user_manage'), 'index.php?app=factory_list&act=activation');
    		}
    	}
    }
    
    /* 分派公司及品牌 */
    function assign_company(){
    	if ($this->visitor->get('role')!=USER_ADMIN){
    		$this->show_warning('user_no_acl');
    		return;
    	}
    	$user_mod =& m('member');
    	$user_id = intval($_GET['uid']);
    	if (!$user_id) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	$user = $user_mod->get(array(
    			'conditions'=>'is_factory=0 and member.user_id='.$user_id
    	));
    	if (!$user) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	if (!IS_POST){
    		$company_mod=& m('company');
    		$companys=$company_mod->find(array(
    			'fields'=>'company_name,company_id',
    			'order'=>'add_time desc',	
    		));
    		$this->assign('user',$user);
    		if ($user['brand_arr_id']){
    			$brand_mod=& m('brand');
    			$b_arr=$brand_mod->find(array(
    				'conditions'=>'company_id='.$user['company_id'],
    				'fields'	=>'brand_id,brand_name'	
    			));
    			$this->assign('b_arr',$b_arr);
    			$arr=explode(',', $user['brand_arr_id']);
    			$this->assign('arr',$arr);
    		}
    		$this->assign('companys',$companys);
    		$this->display('company.assign.html');
    		/* 当前位置 */
    		$this->_curlocal(LANG::get('user_manage'), 'index.php?app=member&act=user_list', Lang::get('assign_company'));
    	}else{
    		if(!$_POST['company_id']){
    			$this->show_warning('select_assign_company');
    			return;
    		}
    		$data=array();
    		$data['company_id']=$_POST['company_id'];
    		$brand_arr=$_POST['brand_arr'];
    		if ($brand_arr){
    			$brand_arr_id=implode(',', $brand_arr);
    			$data['brand_arr_id']=$brand_arr_id;
    		}else{
    			$data['brand_arr_id']=0;
    		}
    		$user_mod->edit($user_id,$data);
    		if ($user_mod->has_error()){
    			$this->show_warning('assign_company_error');
    			return;
    		}
    		$this->show_message('assign_company_success',Lang::get('user_manage'),'index.php?app=member&act=user_list');
    	}
    }
    
    
    //给工厂分派qc
    function assign_qc_info(){
    	if ($this->visitor->get('role')!=USER_ADMIN){
    		$this->show_warning('user_no_acl');
    		return;
    	}
    	$user_mod =& m('member');
    	$user_id = intval($_GET['uid']);
    	if (!$user_id) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	$user = $user_mod->get(array(
    			'join'=>'has_factory',
    			'fields'=>'factory_info.factory_id,user_name,factory_info.factory_name,activation',
    			'conditions'=>'member.user_id='.$user_id.' and role='.USER_FACTORY
    	));
    	if (!$user || !$user['factory_id']) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	$qc_factory_mod=&m('qcfactory');
    	$qcfactory=$qc_factory_mod->find(array(
    			'conditions'=>'factory_id='.$user['factory_id']
    	));
    	if (!IS_POST){
    		$qc_list=$user_mod->find(array(
    				'conditions'=>'activation=1 and role'.db_create_in(array(USER_QC,USER_OUTGOING)),
    				'fields'=>'user_id,user_name',
    				'order'=>'reg_time desc',
    		));
    		$qc_arr=array();
    		if (!empty($qcfactory)){
    			foreach ($qcfactory as $key=>$rows){
    				$qc_arr[]=$rows['user_id'];
    			}
    		}
    		$this->assign('qc_arr',$qc_arr);
    		$this->assign('user',$user);
    		$this->assign('qc_list',$qc_list);
    		$this->display('assign.qc.html');
    		/* 当前位置 */
    		$this->_curlocal(LANG::get('user_manage'), 'index.php?app=member&act=user_list&utype=factory', Lang::get('assign_qc'));
    	}else{
    		$data=array();
    		$qc_arr_id=$_POST['qc_arr'];
    		$data['factory_id']=$user['factory_id'];
    		if (!empty($qc_arr_id)){
    			if (!empty($qcfactory)){
    				foreach($qcfactory as $qc_rows)
    				{
    					if(!in_array($qc_rows['id'],$qc_arr_id))
    					{
    						$qc_factory_mod->drop("id={$qc_rows['id']}");
    					}
    				}
    			}
    			foreach ($qc_arr_id as $res){
    				$data['user_id']=$res;
    				$factory_arr=$qc_factory_mod->get(array(
    						'conditions'=>'factory_id='.$user['factory_id'].' and user_id='.$res
    				));
    				if (empty($factory_arr)){
    					$qc_factory_mod->add($data);
    				}
    			}
    		}
    		if ($qc_factory_mod->has_error()){
    			$this->show_warning('assign_qc_error');
    			return;
    		}
    		$this->show_message('assign_qc_success',Lang::get('user_manage'),'index.php?app=member&act=user_list&utype=factory');
    	}
    }
    
    function show_brand(){
    	$company_id=empty($_GET['company_id'])?0:intval($_GET['company_id']);
    	$brand_mod=& m('brand');
    	$data=array();
    	header("Content-type: text/html; charset=" . CHARSET);
    	if(!$company_id){
    		$data['msg']="网络连接错误，请稍后再试";
    		$data['flag']=false;
    		echo ecm_json_encode($data);
    		return;
    	}
    	$brand_list=$brand_mod->find(array(
    		'conditions'=>'company_id='.$company_id,
    		'fields'	=>'brand_id,brand_name'	
    	));
    	if (!empty($brand_list)){
    		$data['info']=$brand_list;
    		$data['flag']=true;
    	}else{
    		$data['msg']="网络连接错误，请稍后再试";
    		$data['flag']=false;
    		echo ecm_json_encode($data);
    		return;
    	}
    	echo ecm_json_encode($data);
    }
    
    
    //用户设置--用户资料
    function data_info(){
    	$user_info=$this->visitor->get();
    	$user_mod =& m('member');
    	if ($user_info['role']==USER_CLIENT){
    		$user = $user_mod->get(array(
    				'conditions'=>'member.user_id='.$user_info['user_id'],
    				'join'=>'has_consumer'
    		));
    		if (!$user) {
    			$this->show_warning('user_not_exist');
    			return;
    		}
    		if(!IS_POST) {
    			$region_mod=& m('region');
    			
    			//所属区域下拉数据
    			$this->assign('regions',$region_mod->get_options(0));
    			//查询公司及该公司所属的全部品牌
    			if ($user['company_id']){
    				$company_mod=& m('company');
    				$brand_arr=array();
    				$company_info=$company_mod->get(array(
    						'conditions'=>'company_id='.$user['company_id']
    				));
    				$this->assign('company',$company_info);
    				$brand_mod=& m('brand');
    				$brand_list=$brand_mod->find(array(
    						'conditions'=>'company_id='.$user['company_id'],
    						'order'=>'brand_id asc',
    						'limit'=>'10'
    				));
    				if ($user['brand_arr_id']){
    					$brand_arr=explode(',', $user['brand_arr_id']);
    				}
    				$this->assign('brand_arr',$brand_arr);
    				$this->assign('brand_list',$brand_list);
    			}
    			$this->assign('user', $user);
//     			if ($user['freight_id']){
// //     				$merch=$user_mod->get(array(
// //     					'conditions'=>'user_id='.$user['freight_id'].' and role='.USER_MERCHANDISER	
// //     				));
// //     				$this->assign('merch',$merch);
//     				$this->assign('is_user',true);
//     			}
				$this->assign('role_info',$user_info);
    			$this->display('consumer.form.html');
    			/* 当前位置 */
    			$this->_curlocal(LANG::get('user_setting'), 'index.php?app=member&act=data_info', Lang::get('member_data_info'));
    		}else{
    			$email     = trim($_POST['email']);
    			$phone_mob = trim($_POST['phone_mob']);
    			$address   = trim($_POST['address']);
    			$local_data=array();
    			$register_data=array();
    			$local_data['phone_mob']=$phone_mob;
    			$local_data['address']=$address;
    			$local_data['email']=$email;
    			$register_data['consumer_name']=trim($_POST['consumer_name']);
    			$local_data['phone_tel']=trim($_POST['phone_tel']);
    			$local_data['is_qq']=trim($_POST['is_qq']);
    			$register_data['country']=trim($_POST['country']);
    			if ($_POST['region_id']){
    				$local_data['region_id']=trim($_POST['region_id']);
    				$local_data['region_name']=trim($_POST['region_name']);
    			}
    			$register_data['user_type']=trim($_POST['user_type']);
    			//$register_data['cooperate']=trim($_POST['cooperate']);
    			if (!$address || strlen($address)>100){
    				$this->show_warning('address_error');
    				return;
    			}
    		
    			if (!empty($_POST['password']))
    			{
    				$password = trim($_POST['password']);
    				if (strlen($password) < 6 || strlen($password) > 20)
    				{
    					$this->show_warning('password_length_error');
    					return;
    				}
    			}
    			if (!is_email(trim($_POST['email'])))
    			{
    				$this->show_warning('email_error');
    				return;
    			}
    			!empty($_POST['password']) && $local_data['password'] = md5(trim($_POST['password']));
    			$user_mod->edit($user_info['user_id'],$local_data);
    			if ($user_mod->has_error()){
    				$this->show_warning('edit_user_error');
    				return;
    			}
    			$consumer_mod=& m('consumer_info');
    			$consumer_info=$consumer_mod->get(array(
    					'conditions'=>'user_id='.$user_info['user_id']
    			));
    			if (empty($consumer_info)){
    				$register_data['user_id']=$user_info['user_id'];
    				if(!$consumer_mod->add($register_data)){
    					$this->show_warning($consumer_mod->get_error());
    					return;
    				}
    			}else{
    				$consumer_mod->edit('user_id='.$user_info['user_id'],$register_data);
    				if ($consumer_mod->has_error()){
    					$this->show_warning('edit_consumer_error');
    					return;
    				}
    			}
    			$this->show_message('edit_consumer_success');
    		}
    	}elseif ($user_info['role']==USER_FACTORY){
	    	$upload_mod =& m('upload');
	    	$user = $user_mod->get(array(
	    			'conditions'=>'member.user_id='.$user_info['user_id'],
	    			'join'=>'has_factory'
	    	));
	    	if (!$user) {
	    		$this->show_warning('user_not_exist');
	    		return;
	    	}
	    	if(!IS_POST) {
	    		$region_mod=& m('region');
	    		$this->assign('regions',$region_mod->get_options(0));
	    		$this->assign('user', $user);
	    		//赋值session_id
	    		$this->assign('phpsision', session_id());
	    		//查询当前session_id下的上传图片
	    		$img_file=$upload_mod->find(array(
	    				'conditions'=>"file_type=".BELONGS_FACTORY_IMAGE." and key_id=".$user_info['factory_id']
	    		));
	    		if ($img_file){
	    			$this->assign('files',$img_file);
	    		}
	    		$this->assign('role_info',$user_info);
	    		$this->display('factory_consumer.form.html');
	    		/* 当前位置 */
	    		$this->_curlocal(LANG::get('user_setting'), 'index.php?app=member&act=data_info', Lang::get('member_data_info'));
	    	}else{
	    		$email     = trim($_POST['email']);
	    		$phone_mob = trim($_POST['phone_mob']);
	    		$address   = trim($_POST['address']);
	    		$local_data=array();
	    		$local_data['phone_mob']=$phone_mob;
	    		$local_data['phone_tel']=trim($_POST['phone_tel']);
	    		$local_data['address']=$address;
	    		$local_data['email']=$email;
	    		$local_data['region_id']=trim($_POST['region_id']);
	    		$local_data['region_name']=trim($_POST['region_name']);
	    		$local_data['is_qq']=trim($_POST['is_qq']);
	    		$register_data=array();
	    		$register_data['demo']=trim($_POST['demo']);
	    		$register_data['factory_name']=trim($_POST['factory_name']);
	    		$register_data['factory_charge']=trim($_POST['factory_charge']);
	    		//$register_data['cooperate']=trim($_POST['cooperate']);
	    		$register_data['bank_account']=trim($_POST['bank_account']);
	    		$register_data['bank_user']=trim($_POST['bank_user']);
	    		$register_data['bank_name']=trim($_POST['bank_name']);
	    		if (empty($_POST['region_name'])){
	    			$this->show_warning('region_required');
	    			
	    			return;
	    		}
	    		
	    		if (!$address || strlen($address)>100){
	    			$this->show_warning('address_error');
	    			return;
	    		}
	    
	    		if (!empty($_POST['password']))
	    		{
	    			$password = trim($_POST['password']);
	    			if (strlen($password) < 6 || strlen($password) > 20)
	    			{
	    				$this->show_warning('password_length_error');
	    				return;
	    			}
	    		}
	    		if (!is_email(trim($_POST['email'])))
	    		{
	    			$this->show_warning('email_error');
	    			return;
	    		}
	    		!empty($_POST['password']) && $local_data['password'] = md5(trim($_POST['password']));
	    		$user_mod->edit($user_info['user_id'],$local_data);
	    		if ($user_mod->has_error()){
	    			$this->show_warning('edit_user_error');
	    			return;
	    		}
	    		$factory_info_mod=& m('factory_info');
	    		$factory_info=$factory_info_mod->get(array(
	    				'conditions'=>'user_id='.$user_info['user_id']
	    		));
	    		if (empty($factory_info)){
	    			$register_data['user_id']=$user_info['user_id'];
	    			$factory_id=$factory_info_mod->add($register_data);
	    			if(!$factory_id){
	    				$this->show_warning($factory_info_mod->get_error());
	    				return;
	    			}
	    		}else{
	    			$factory_id=$factory_info['factory_id'];
	    			$factory_info_mod->edit('user_id='.$user_info['user_id'],$register_data);
	    			if ($factory_info_mod->has_error()){
	    				$this->show_warning('edit_factory_info_error');
	    				return;
	    			}
	    		}
	    		$upfiles=$_POST['upfiles'];
	    		
	    		if(!empty($upfiles))
	    		{
	    			$upload_mod->edit("file_id ".db_create_in($upfiles),array('key_id'=>$factory_id,'session_id'=>session_id(),'file_type'=>BELONGS_FACTORY_IMAGE));
	    		}
	    		$this->show_message('edit_factory_info_success');
	    	}
    	}elseif ($user_info['role']==USER_MERCHANT){
    		$uid=$user_info['user_id'];
    		$fs_mod=&m('fabric_shop');
    		$user_mod=&m('member');
    		if (!IS_POST){
    			$merch=$fs_mod->get_merch_info($uid);
    			$this->assign('fabric_types', $this->_get_options());
    			$this->assign('merch',$merch);
    			$this->assign('user_id',$uid);
    			$this->assign('user',$user_info);
    			$this->display('member.merch.html');
    			/* 当前位置 */
    			$this->_curlocal(LANG::get('member_manage'), 'index.php?app=fabric_shop&act=activation', Lang::get('edit_merch_info'));
    		}else{
    			$data=array();
    			$merch_data=array();
    			$email=trim($_POST['email']);
    			$data['type_id']=$_POST['type_id'];
    			$data['description']=$_POST['description'];
    			$data['if_show']=$_POST['if_show'];
    			$merch_data['phone_tel']=trim($_POST['phone_tel']);
    			$merch_data['phone_mob']=trim($_POST['phone_mob']);
    			$merch_data['address']=trim($_POST['address']);
    			$merch_data['email']=$email;
    			$merch_data['is_qq']=trim($_POST['is_qq']);
    			$merch_data['wx_sn']=trim($_POST['wx_sn']);
				$merch_data['ftype_id']=trim($_POST['type_id']);
    			if ($email){
    				if (!is_email($email)) {
    					$this->show_warning('email_error');
    					return;
    				}
    			}
    			if (!empty($_POST['password']))
    			{
    				$password = trim($_POST['password']);
    				if (strlen($password) < 6 || strlen($password) > 20)
    				{
    					$this->show_warning('password_length_error');
    					return;
    				}
    			}
    			!empty($_POST['password']) && $merch_data['password'] = md5(trim($_POST['password']));
    		
    			$user_mod->edit($uid,$merch_data);
    			if ($user_mod->has_error()){
    				$this->show_warning($user_mod->get_error());
    				return;
    			}
    			$fs_mod->edit('user_id='.$uid,$data);
    			if ($fs_mod->has_error()){
    				$this->show_warning($fs_mod->get_error());
    				return;
    			}
    			$this->show_message('供应商编辑成功!');
    		}
    	}else{
    		$this->show_message('no_powers_to_visit');
    		return;
    	}
    }
    
    
    function view_user(){
    	$user_info=$this->visitor->get('role');
    	if (in_array($user_info, array(USER_MANAGER,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN))){
    		$this->assign('is_edit',true);
    	}
    	$user_mod =& m('member');
    	$user_id = intval($_GET['uid']);
    	if (!$user_id) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	$member_info = $user_mod->get(array(
    			'conditions'=>'member.user_id='.$user_id,
    			'join'=>'has_consumer'
    	));
    	if (empty($member_info)) {
    		$this->show_warning('user_not_exist');
    		return;
    	}
    	if (!IS_POST){
    		$region_mod=& m('region');
    		$freight_list=$user_mod->find(array(
    				'conditions'=>'activation=1 and role='.USER_MERCHANDISER,
    				'order'		=>'reg_time'
    		));
    		$this->assign('freights',$freight_list);
    		//所属区域下拉数据
    		$this->assign('regions',$region_mod->get_options(0));
    		//查询公司及该公司所属的全部品牌
    		if ($member_info['company_id']){
    			$company_mod=& m('company');
    			$brand_arr=array();
    			$company_info=$company_mod->get(array(
    					'conditions'=>'company_id='.$member_info['company_id']
    			));
    			$this->assign('company',$company_info);
    			$brand_mod=& m('brand');
    			$brand_list=$brand_mod->find(array(
    					'conditions'=>'company_id='.$member_info['company_id'],
    					'order'=>'brand_id asc',
    					'limit'=>'10'
    			));
    			if ($member_info['brand_arr_id']){
    				$brand_arr=explode(',', $member_info['brand_arr_id']);
    			}
    			$this->assign('brand_arr',$brand_arr);
    			$this->assign('brand_list',$brand_list);
    		}
    		$this->assign('member_info', $member_info);
    		$this->display('consumer.view.html');
    		/* 当前位置 */
    		$this->_curlocal("用户列表", 'index.php?app=schedule&act=consumer_list', "查看用户");
    	}else{
    		$data=array();
    		$consumer_info_mod=&m('consumer_info');
    		$data['consumer_demo']=$_POST['consumer_demo'];
    		$consumer_info_mod->edit('user_id='.$user_id,$data);
    		if ($consumer_info_mod->has_error()){
    			$this->show_warning($consumer_info_mod->get_error());
    			return;
    		}
    		$this->show_message("修改成功",'客户列表','index.php?app=schedule&act=consumer_list');
    	}
    }
    
    /* 构造并返回树 */
    function &_tree($types)
    {
    	import('tree.lib');
    	$tree = new Tree();
    	$tree->setTree($types, 'fabric_type_id', 'parent_id', 'fabric_type_name');
    	return $tree;
    }
    
    /* 取得可以作为上级的类型数据 */
    function _get_options($except = NULL)
    {
    	$ft_mod=&m('fabric_type');
    	$types = $ft_mod->get_list();
    	$tree =& $this->_tree($types);
    	return $tree->getOptions(MAX_LAYER - 1, 0, $except);
    }
}

?>
